package com.cmq.web_1.muitiTable;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/multiplication")
public class MultiTableServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置响应内容类型为HTML，并指定UTF-8编码
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        // 生成HTML页面
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>九九乘法表</title>");
        // 添加CSS样式，设置表格样式和间隔底色
        out.println("<style>");
        out.println("table { border-collapse: collapse; margin: 20px auto; }");
        out.println("td {");
        out.println("    border: 1px solid #333;");
        out.println("    padding: 8px 15px;");
        out.println("    text-align: center;");
        out.println("    font-size: 16px;");
        out.println("}");
        // 间隔行不同底色
        out.println(".even { background-color: #f0f8ff; }");
        out.println(".odd { background-color: #e6f2ff; }");
        out.println("h1 { text-align: center; color: #333; }");
        out.println("</style>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>九九乘法表</h1>");
        out.println("<table>");

        // 生成倒三角九九乘法表
        for (int i = 9; i >= 1; i--) {
            // 根据行号设置不同的底色类
            String rowClass = (i % 2 == 0) ? "even" : "odd";
            out.println("<tr class='" + rowClass + "'>");

            for (int j = 1; j <= i; j++) {
                out.println("<td>");
                out.println(j + "×" + i + "=" + (j * i));
                out.println("</td>");
            }

            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
    }
}
